/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 */
package com.gys.common.util;

import cn.hutool.core.date.DateUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

public class PingIpUtil {
    public static boolean isConnect(String ip) {
        boolean connect = false;
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec("ping " + ip);
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "GBK");
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            isr.close();
            br.close();
            if (!sb.toString().equals("") && sb.toString().indexOf("TTL") > 0) {
                connect = true;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return connect;
    }

    public static boolean isConnect() {
        return PingIpUtil.isConnect("www.baidu.com");
    }

    public static boolean isReachable(String remoteInetAddr) {
        boolean reachable = false;
        try {
            InetAddress address = InetAddress.getByName(remoteInetAddr);
            reachable = address.isReachable(5000);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return reachable;
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.now());
        System.out.println("ping\u7ed3\u679c:" + PingIpUtil.isReachable("www.baidu.com"));
        System.out.println(DateUtil.now());
        System.out.println(DateUtil.now());
        System.out.println("ping\u7ed3\u679c:" + PingIpUtil.isConnect("www.baidu.com"));
        System.out.println(DateUtil.now());
    }
}

